<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Form
 *
 * Create dynamical forms for a website, like contact form
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Form extends User_Controller {
	function Form( ) {
		parent::User_Controller( );
		
		$this->load->helper( 'form' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Form' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the form  model
		$this->load->model( 'form_model' );
		$this->load->module_model( 'article', 'website_model' );
		
		// Load the form module
		$this->lang->module_load( 'form', 'form' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'form' ), 'form/user/form' );
				
		log_message( 'debug', 'conlabz CMS : Form class loaded' );
	}	
	
	/**
	 * View Form
	 *
	 * @access public
	 */
	function index( ) {
		// Get forms
		$data['form']   = $this->form_model->getForms( );
		
		// Display Page
		$data['header'] = $this->lang->line( 'form_overview' );
		$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/view_form";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'form';
		
		$this->load->module_view( 'article', 'user/container', $data );
	}
		
	/**
	 * Get form Details
	 *
	 * Load form detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_form_details( ) {
		$data['id']    = $this->input->post( 'id'    );
		$data['title'] = $this->input->post( 'title' );
		$data['description'] = $this->input->post( 'description' );
		$data['email'] = $this->input->post( 'email' );
		
		return $data;
	}
	
	/**
	 * Display Form Form
	 *
	 * @access public
	 * @param integer $id Form ID
	 */
	function newform( $id = NULL ) {
		// VALIDATION FIELDS
		$fields['id']    = "ID";
		$fields['title'] = $this->lang->line( 'form_title' );
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['title'] = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['title'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$form = $this->form_model->getForms( array( 'form.id' => $id ) );
			$form = $form->row_array( );
			$this->validation->set_default_value( 'form', $form['id'] );
			$this->validation->set_default_value( $form );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ) {
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			
			$this->validation->output_errors( );
			$data['fckeditor'] = array(
              	'name'        => 'description',
              	'id'          => 'description',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config' => array( 'CustomConfigurationsPath' => DEVPATH.'template/'.$this->session->userdata( 'template' ).'/js/fckconfig.js' )
			);
    		
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_form' ) : $this->lang->line( 'edit_form' ) );
			$this->bep_site->set_crumb( $data['header'], 'form/user/form/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_form";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'form';
			$this->load->module_view( 'article', 'user/container', $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$form = $this->_get_form_details( );
				
				$this->db->trans_start( );
				$form['form_id'] = $this->form_model->insert( $form );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $form['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_user' ) ) );
				}
				redirect( 'form/user/form' );
			} else {
				// SAVE
				$form = $this->_get_form_details( );
				
				$this->db->trans_start( );
				$this->form_model->update( $form, $form['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $form['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect( 'form/user/form' );
			}
		}
	} // form( );
	
	/**
	 * Delete
	 *
	 * Delete the selected form from the system
	 *
	 * @access public
	 */
	function delete( ) {
		$this->load->model( 'form_model' );	
		$entry_id = $this->form_model->_getEntryID( );
		$this->form_model->delete_entry( $entry_id );
		
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'form/user/form', 'location' );
		}

		foreach( $selected as $form ) {
			$this->db->delete( 'form', array( 'id' => $form ) );
		}

		flashMsg( 'success', $this->lang->line( 'form_deleted' ) );
		redirect( 'form/user/form', 'location' );
	}
	
	/**
	 * View form fields
	 * 
	 * @access public
	 */
	function showfields( $form_id ) {
		// Get entries
		$fields  = $this->form_model->getFormFields( $form_id );
		$row = $fields->result_array( );
		if( !empty ( $row[0]['label'] ) ){
			$label = unserialize( $row[0]['label'] );
		}
		if( !empty ( $row[0]['type'] ) ){
			$type = unserialize( $row[0]['type'] );
		}
		if( !empty ( $row[0]['name'] ) ){
			$name = unserialize( $row[0]['name'] );
		}
		if( !empty( $label ) ){
			$id = 1;
			foreach( $label as $key => $value ){
				$data['fields'][$key]['id']    = $id;
				$data['fields'][$key]['label'] = $value;
				$data['fields'][$key]['type']  = $type[$key];
				$data['fields'][$key]['name']  = $name[$key];
				$id++;
			}	
		}
		$data['form_id'] = $form_id;
		// Display Page
		$data['header'] = $this->lang->line( 'form_field_overview' );
		$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/view_fields";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'form';
		
		$this->load->module_view( 'article', 'user/container', $data );
	}
	
	/**
	 * Get form entry details
	 * 
	 * @access private
	 * @return array
	 */
	function _get_field_details( ) {
		$data['field_id'] = $this->input->post( 'field_id' );
		$data['label']    = $this->input->post( 'label'    );
		$data['type'] 	  = $this->input->post( 'type'     );
		
		return $data;
	}
	
	/**
	 * Display Form Fields
	 *
	 * @access public
	 * @param integer $form_id Form ID
	 * @param integer $field_id Form Field ID
	 */
	function newfield( $form_id, $field_id = NULL ) {
		// VALIDATION FIELDS
		$fields['field_id']   = "ID";
		$fields['label']      = $this->lang->line( 'form_field_label'      );
		$fields['type']       = $this->lang->line( 'form_field_type'       );
		$fields['name']       = $this->lang->line( 'form_field_name'       );
		$fields['value']      = $this->lang->line( 'form_field_value'      );
		$fields['additional'] = $this->lang->line( 'form_field_additional' );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $field_id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['label'] = "trim|required";
			$rules['type']  = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['label'] = "trim|required";
			$rules['type']  = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $field_id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$fields = $this->form_model->getFormFields( $form_id );
			$fields = $fields->row_array( );
			//$this->validation->set_default_value( 'entry', $entry['entry_id'] );
			//$this->validation->set_default_value( $entry );
		} elseif( is_null( $field_id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			$data['form_id'] = $form_id;
			$data['type_options'] = array( 'text' => 'Text',
								 		   'textarea' => 'Textarea',
										   'checkbox' => 'Checkbox' );
			$this->validation->output_errors( );
			$data['header'] = (is_null( $field_id ) ? $this->lang->line( 'create_form_field' ) : $this->lang->line( 'edit_form_field' ) );
			$this->bep_site->set_crumb( $data['header'], 'form/user/form/newfield/'.$form_id.'/'.$field_id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_fields";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'form';
			$this->load->module_view( 'article', 'user/container', $data );
		} else {
			// Save form
			if( is_null( $field_id ) ) {
				// CREATE
				// Fetch form values
				$field = $this->_get_field_details( );
				$field['form_id'] = $form_id;
				$this->db->trans_start( );
				$field['field_id'] = $this->form_model->update_field( $form_id, $field );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit();
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $field['label'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_user' ) ) );
				}
				//@todo: specify $form_id
				redirect( 'form/user/form/showfields/'.$form_id );
			} else {
				// SAVE
				$field = $this->_get_field_details( );
				$field['form_id'] = $form_id;				
				$this->db->trans_start( );
				$this->form_model->update_field( $form_id, $field, $field['field_id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $field['label'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				//@todo: specify $form_id
				redirect( 'form/user/form/showfields/'.$form_id );
			}
		}
	} // form_entries( );
	
	/**
	 * Delete
	 *
	 * Delete the selected entry from the system
	 *
	 * @access public
	 */
	function delete_entry( ) {
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'form/user/form', 'location' );
		}

		foreach( $selected as $entry ) {
			$this->db->delete( 'form_entry', array( 'entry_id' => $entry ) );
		}

		flashMsg( 'success', $this->lang->line( 'form_entry_deleted' ) );
		redirect( 'form/user/form', 'location' );
	}
}
/* End of file form.php */
/* Location: ./modules/form/controllers/user/form.php */